package com.icinfo.cloud.provider.punish.common.publicsecurityentrust.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.utils.HeaderUtil;
import com.icinfo.cloud.provider.punish.common.publicsecurityentrust.service.MemberApiService;
import com.icinfo.cloud.provider.punish.common.publicsecurityentrust.vo.MemberDeptVo;
import com.icinfo.cloud.provider.punish.common.publicsecurityentrust.vo.MemberOrgVo;
import com.icinfo.framework.common.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * 检查接口方法
 * @author HKxxx
 * @date 2024/2/2 14:19
 */
@Slf4j
@Service
public class MemberApiServiceImpl implements MemberApiService {


    @Value("${member.url}")
    private String memberUrl;


    @Value("${member.api.getOrgNameByOrgCode}")
    private String getOrgNameByOrgCodeUrl;

    @Value("${member.api.getDeptNameByDeptCode}")
    private String getDeptNameByDeptCodeUrl;




    @Override
    public Map<String, MemberOrgVo> getOrgNameByOrgCode(List<String> orgCodes) {
        if (CollUtil.isEmpty(orgCodes)){
            throw new BusinessException("获取单位名称信息异常：orgCodes不能为空");
        }
        Map<String, String> headers = HeaderUtil.authHeader();
        String json = JSONObject.toJSONString(orgCodes);
        String response = HttpUtil.createPost(memberUrl.concat(getOrgNameByOrgCodeUrl)).addHeaders(headers).body(json).execute().body();
        JSONObject resultObject = JSONObject.parseObject(response);
        if (HttpStatus.HTTP_OK != resultObject.getInteger("code")) {
            throw new BusinessException("获取单位名称信息异常：" + resultObject.getString("message"));
        }
        List<MemberOrgVo> data = JSON.parseArray(resultObject.getString("data"), MemberOrgVo.class);
        if (CollUtil.isEmpty(data)){
            throw new BusinessException("获取单位名称信息异常：" + "无单位名称信息");
        }
        return data.stream().collect(Collectors.toMap(MemberOrgVo::getOrgCode, Function.identity()));
    }

    @Override
    public Map<String, MemberDeptVo> getDeptNameByDeptCode(List<String> deptCodes) {
        if (CollUtil.isEmpty(deptCodes)){
            throw new BusinessException("获取单位部门名称信息异常：deptCodes不能为空");
        }
        Map<String, String> headers = HeaderUtil.authHeader();
        String json = JSONObject.toJSONString(deptCodes);
        String response = HttpUtil.createPost(memberUrl.concat(getDeptNameByDeptCodeUrl)).addHeaders(headers).body(json).execute().body();
        JSONObject resultObject = JSONObject.parseObject(response);
        if (HttpStatus.HTTP_OK != resultObject.getInteger("code")) {
            throw new BusinessException("获取单位部门名称信息异常：" + resultObject.getString("message"));
        }
        List<MemberDeptVo> data = JSON.parseArray(resultObject.getString("data"), MemberDeptVo.class);
        if (CollUtil.isEmpty(data)){
            throw new BusinessException("获取单位部门名称信息异常：" + "无单位部门名称信息");
        }
        return data.stream().collect(Collectors.toMap(MemberDeptVo::getDeptCode, Function.identity()));
    }
}
